﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agos.Utils;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using CFG = System.Configuration;

namespace DeutscheRemoval.Utils
{
    class MailUtils
    {
        // Send a mail
        public static void SendMail(string from, string to, string subject, string body, string filePath)
        {
            try
            {
                using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress(from);
                    mail.To.Add(new MailAddress(to));
                    mail.Subject = subject;
                    mail.Body = body;

                    Attachment messageAttachment = new Attachment(filePath); //excel file read from disk
                    mail.Attachments.Add(messageAttachment);

                    SmtpClient Smtp_Client = new SmtpClient();
                    Smtp_Client.Port = int.Parse(CFG.ConfigurationManager.AppSettings["Port"]);
                    Smtp_Client.Host = CFG.ConfigurationManager.AppSettings["SMTP_Server"];
                    if (CFG.ConfigurationManager.AppSettings["SMTP_Server"] == "Y")
                        Smtp_Client.EnableSsl = true;
                    else Smtp_Client.EnableSsl = false;

                    if (CFG.ConfigurationManager.AppSettings["SMTP_Server"].Contains("gmail"))
                    {
                        Smtp_Client.EnableSsl = true;
                    }

                    Smtp_Client.Credentials = new NetworkCredential(CFG.ConfigurationManager.AppSettings["Sender"],
                                                                    CFG.ConfigurationManager.AppSettings["Password"]);
                    Smtp_Client.Timeout = 300000;

                    //Smtp_Client.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
                    //Smtp_Client.SendAsync(mail, body); 
                    try
                    {
                        Smtp_Client.Send(mail);
                        string today = DateTime.Now.ToString("dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
                        Logger.Log(today + "Message sent to: " + CFG.ConfigurationManager.AppSettings["StatusEmail"], LogType.Operation);
                    }
                    catch (Exception e)
                    {
                        string today = DateTime.Now.ToString("dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
                        Logger.Log(today + e.Message.ToString(), LogType.Error);
                    }
                    finally
                    {
                        foreach (var attachment in mail.Attachments)
                            attachment.Dispose();
                    }
                }
                }
            catch (Exception ex)
            {
                Logger.Log(ex.Message,LogType.Error);
            }
        }

        private static void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            String token = (string)e.UserState;
            string today = DateTime.Now.ToString("dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);

            if (e.Error != null)
            {
                Logger.Log(today + "Mail sending canceled" + token + " " + e.Error.ToString(), LogType.Error);
            }
            else if (e.Cancelled)
            {
                Logger.Log(today + "Error while sending mail: " + token + "  " + e.Error.ToString(), LogType.Error);
            }
            else
            {
                Logger.Log(today + "Message sent " + token, LogType.Error);
            }
        }

    }
}
